﻿package mortal.game.view.petNew
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.palyer.view.*;
    import mortal.game.view.petNew.view.*;
    import mortal.game.view.playerSystem.*;
    import mortal.game.view.rebate.openServerSevenDay.data.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class NewPetController extends PlayerBaseController
    {
        private var _moduel:NewPetModule;
        private var cache:Cache;

        public function NewPetController()
        {
            this.cache = Cache.instance;
            return;
        }// end function

        private function onPetAdvance(event:DataEvent) : void
        {
            GameProxy.pet.advance(event.data.autoBuy, event.data.items);
            return;
        }// end function

        protected function onPetInfoUpdate(param1:Object) : void
        {
            this._moduel.updatePet();
            this.updateMountAvtivityWin();
            this.shapeCardStatUpdate(true);
            return;
        }// end function

        private function updateMountAvtivityWin(param1:Object = null) : void
        {
            var _loc_2:TMountConfig = null;
            var _loc_3:ChargeActivityData = null;
            if (Cache.instance.newPet.pet.code < 3)
            {
                if (ClockManager.instance.serverOpenDateNum <= 7 && Cache.instance.openServerSevenDay.getTargetTypeName() == "" + Language.getString(71763) && Cache.instance.rebate.rechargeSum == 0)
                {
                    ChargeActivityWin.instance.show(window.x + window.width + 8, window.y);
                    ChargeActivityWin.instance.layer.setTop(this.window);
                    _loc_2 = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, 3);
                    _loc_3 = new ChargeActivityData();
                    _loc_3.activityType = ChargeActivityData.ActivityType_Three;
                    _loc_3.tmount = _loc_2;
                    ChargeActivityWin.instance.data = _loc_3;
                    if (PetActivityPanel.isViewShow())
                    {
                        PetActivityPanel.instance.hide();
                    }
                }
                else
                {
                    if (!PetActivityPanel.isViewShow())
                    {
                        if (OpenServerSevenDayUtil.isNotOpenSpeActive())
                        {
                            return;
                        }
                        PetActivityPanel.instance.show();
                        PetActivityPanel.instance.show(window.x + window.width + 4, window.y + 37);
                        PetActivityPanel.instance.layer.setTop(this.window);
                    }
                    if (ChargeActivityWin.isViewShow)
                    {
                        ChargeActivityWin.instance.hide();
                    }
                }
            }
            else if (Cache.instance.newPet.pet.code == 3)
            {
                if (PetActivityPanel.isViewShow())
                {
                    PetActivityPanel.instance.hide();
                }
                if (ClockManager.instance.serverOpenDateNum <= 7 && Cache.instance.openServerSevenDay.getTargetTypeName() == "" + Language.getString(71763) && Cache.instance.rebate.rechargeSum < 500)
                {
                    ChargeActivityWin.instance.show(window.x + window.width + 8, window.y);
                    ChargeActivityWin.instance.layer.setTop(this.window);
                    _loc_2 = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, 4);
                    _loc_3 = new ChargeActivityData();
                    _loc_3.activityType = ChargeActivityData.ActivityType_Four;
                    _loc_3.tmount = _loc_2;
                    ChargeActivityWin.instance.data = _loc_3;
                }
            }
            if (BuyGiftItemWin.isViewShow)
            {
                BuyGiftItemWin.instance.layer.setTop(BuyGiftItemWin.instance);
            }
            return;
        }// end function

        private function mountChangeSharp(event:DataEvent) : void
        {
            GameProxy.pet.changeShapeCard();
            return;
        }// end function

        private function onPetMoudelShow(event:DataEvent) : void
        {
            if (this.cache.newPet.pet.code < 3)
            {
                if (OpenServerSevenDayUtil.isNotOpenSpeActive())
                {
                    return;
                }
                PetActivityPanel.instance.show();
                PetActivityPanel.instance.show(this.window.x + this.window.width + 5, this.window.y + 37);
                PetActivityPanel.instance.layer.setTop(this.window);
            }
            else if (PetActivityPanel.hasInstance() && PetActivityPanel.instance.isHide == false)
            {
                PetActivityPanel.instance.hide();
            }
            return;
        }// end function

        private function onPetStudyDirectSkill(event:DataEvent) : void
        {
            var itemData:ItemData;
            var curSkill:TSkill;
            var skillId:int;
            var skillName:String;
            var onAlertClose:Function;
            var e:* = event;
            onAlertClose = function (param1:int) : void
            {
                if (param1 == Alert.YES)
                {
                    GameProxy.pet.learnSkillDirect(itemData.uid);
                }
                return;
            }// end function
            ;
            itemData = e.data as ItemData;
            var skillInfo:* = SkillConfig.instance.getInfoById(itemData.itemInfo.effect);
            var _loc_3:int = 0;
            var _loc_4:* = this.cache.newPet.pet.skillIds;
            while (_loc_4 in _loc_3)
            {
                
                skillId = _loc_4[_loc_3];
                curSkill = SkillConfig.instance.getInfoById(skillId);
                if (curSkill.series == skillInfo.series && curSkill.skillLevel >= skillInfo.skillLevel)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71768));
                    return;
                }
            }
            skillName = HTMLUtil.addColor(skillInfo.name, ColorConfig.instance.getItemColor(skillInfo.skillLevel).color);
            Alert.show(Language.getStringByParam(71769, skillName), null, Alert.YES | Alert.NO, null, onAlertClose);
            return;
        }// end function

        private function onUseSkillBook(event:DataEvent) : void
        {
            var _loc_6:TSkill = null;
            var _loc_7:TMountConfig = null;
            var _loc_8:Array = null;
            var _loc_9:SkillInfo = null;
            var _loc_10:int = 0;
            var _loc_14:SkillInfo = null;
            var _loc_15:int = 0;
            var _loc_16:SkillInfo = null;
            var _loc_2:* = event.data as ItemData;
            var _loc_3:* = SkillConfig.instance.getInfoById(_loc_2.itemInfo.effect);
            var _loc_4:* = MountConfig.instance.getFashionsByType(EAdvanceComponentType._EAdvanceComponentTypePet);
            if (Cache.instance.newPet.pet == null)
            {
                return;
            }
            var _loc_5:* = Cache.instance.newPet.pet.code;
            for each (_loc_7 in _loc_4)
            {
                
                if (_loc_7.skillList)
                {
                    _loc_6 = SkillConfig.instance.getInfoById(int(_loc_7.skillList));
                    if (_loc_6.posType == _loc_3.posType)
                    {
                        if (_loc_5 < _loc_7.code)
                        {
                            return MsgManager.showRollTipsMsg(Language.getStringByParam(71770, _loc_7.code), MsgRollTipsType.msgRollTips3);
                        }
                    }
                }
            }
            _loc_8 = [];
            _loc_10 = 0;
            while (_loc_10 < 8)
            {
                
                if (Cache.instance.newPet.pet.code >= (_loc_15 + 1))
                {
                    _loc_9 = Cache.instance.newPet.getPetSkill(_loc_15 + PetConst.PASSIVE_SKILL_START_POS);
                    if (_loc_9)
                    {
                        _loc_8.push(_loc_9);
                    }
                }
                _loc_10++;
            }
            var _loc_11:* = _loc_8.length > 0 ? (false) : (true);
            var _loc_12:Boolean = true;
            var _loc_13:* = new Dictionary();
            _loc_15 = 0;
            while (_loc_15 < _loc_8.length)
            {
                
                _loc_16 = _loc_8[_loc_15];
                _loc_14 = _loc_13[_loc_16.tSkill.posType] as SkillInfo;
                if (_loc_14 == null || _loc_14 && _loc_16.tSkill.skillLevel > _loc_14.tSkill.skillLevel)
                {
                    _loc_13[_loc_16.tSkill.posType] = _loc_16;
                }
                _loc_15++;
            }
            for each (_loc_9 in _loc_13)
            {
                
                if (_loc_3.posType == _loc_9.tSkill.posType)
                {
                    if (_loc_3.skillLevel <= _loc_9.tSkill.skillLevel)
                    {
                        return MsgManager.showRollTipsMsg("" + Language.getString(71771), MsgRollTipsType.msgRollTips3);
                    }
                    if (_loc_3.skillLevel - _loc_9.tSkill.skillLevel > 1)
                    {
                        return MsgManager.showRollTipsMsg("" + Language.getString(71772), MsgRollTipsType.msgRollTips3);
                    }
                    break;
                }
            }
            if (PetInfoPanel.isShowNow == false)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PlayerModuleShowIndexByType, PlayerConst.TabType_Pet));
            }
            GameProxy.pet.learnSkillDirect(_loc_2.serverData.uid);
            return;
        }// end function

        private function onPetAdvanceRewardGet(event:DataEvent) : void
        {
            GameProxy.pet.getAdvanceReward(event.data as int);
            return;
        }// end function

        private function petActivityPanelHide(param1:Object = null) : void
        {
            if (PetActivityPanel.hasInstance() && !PetActivityPanel.instance.isHide)
            {
                PetActivityPanel.instance.hide();
            }
            if (ChargeActivityWin.isViewShow)
            {
                ChargeActivityWin.instance.hide();
            }
            return;
        }// end function

        protected function onViewShow(event:Event) : void
        {
            this.onPetMoudelShow(null);
            this.updateMountAvtivityWin();
            Dispatcher.addEventListener(EventName.PetAdvance, this.onPetAdvance);
            NetDispatcher.addCmdListener(ServerCommand.NewPetAttributeUpdate, this.onPetInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updatePetItem);
            NetDispatcher.addCmdListener(ServerCommand.UpdatePetEquip, this.updateEquip);
            EventUtil.addNetEvent(ServerCommand.PetSharpSuccess, this.onPetSharpChange, true);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.updateVip);
            Dispatcher.addEventListener(EventName.MountUseGrouthItem, this.mountUseGrouthItem);
            Dispatcher.addEventListener(EventName.MountUseTalentItem, this.mountUseTalentItem);
            NetDispatcher.addCmdListener(ServerCommand.RebateDayChargeInfoBack, this.updateMountAvtivityWin);
            Dispatcher.addEventListener(EventName.PetChangeSharp, this.mountChangeSharp);
            return;
        }// end function

        protected function onViewHide(event:Event) : void
        {
            this.petActivityPanelHide();
            Dispatcher.removeEventListener(EventName.PetAdvance, this.onPetAdvance);
            NetDispatcher.removeCmdListener(ServerCommand.NewPetAttributeUpdate, this.onPetInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updatePetItem);
            NetDispatcher.removeCmdListener(ServerCommand.UpdatePetEquip, this.updateEquip);
            EventUtil.addNetEvent(ServerCommand.PetSharpSuccess, this.onPetSharpChange, false);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.updateVip);
            Dispatcher.removeEventListener(EventName.MountUseGrouthItem, this.mountUseGrouthItem);
            Dispatcher.removeEventListener(EventName.MountUseTalentItem, this.mountUseTalentItem);
            NetDispatcher.removeCmdListener(ServerCommand.RebateDayChargeInfoBack, this.updateMountAvtivityWin);
            Dispatcher.removeEventListener(EventName.PetChangeSharp, this.mountChangeSharp);
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.PetAdvanceRewardGet, this.onPetAdvanceRewardGet);
            Dispatcher.addEventListener(EventName.PetMoudelShow, this.onPetMoudelShow);
            Dispatcher.addEventListener(EventName.PetStudyDirectSkill, this.onPetStudyDirectSkill);
            NetDispatcher.addCmdListener(ServerCommand.PetGetAdvanceRewardSucceed, this.petActivityPanelHide);
            Dispatcher.addEventListener(EventName.PetEquip, this.petEquip);
            Dispatcher.addEventListener(EventName.PetNewUseSkillBook, this.onUseSkillBook);
            Dispatcher.addEventListener(EventName.BestShowPetWin, this.openWin);
            Dispatcher.addEventListener(EventName.PetNewPopUpWin, this.popupWinHandler);
            return;
        }// end function

        private function onPetSharpChange(param1:Object) : void
        {
            if (this._moduel)
            {
                this._moduel.updateMountSelect();
            }
            return;
        }// end function

        private function updateVip(param1:Object) : void
        {
            this._moduel.updateVip();
            return;
        }// end function

        private function mountUseGrouthItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemCode);
            return;
        }// end function

        private function mountUseTalentItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemCode);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            GameManager.instance.popupModuleType(ModuleType.Pets);
            var _loc_2:* = event.data as String;
            if (this.isViewShow && PlayerConst.TabType_Pet == _loc_2)
            {
                this._moduel.showBtnFilter();
            }
            return;
        }// end function

        private function popupWinHandler(param1) : void
        {
            if (this.isViewShow)
            {
                GameController.playerSystem.view.hide();
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PlayerModuleShowIndexByType, PlayerConst.TabType_Pet));
            }
            return;
        }// end function

        override public function addWinShowEvent() : void
        {
            this.onViewShow(null);
            return;
        }// end function

        override public function removeWinShowEvent() : void
        {
            this.onViewHide(null);
            return;
        }// end function

        override protected function initView() : GSprite
        {
            if (!this._moduel)
            {
                this._moduel = new NewPetModule();
                this._moduel.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._moduel.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return this._moduel;
        }// end function

        private function petEquip(event:DataEvent) : void
        {
            var _loc_2:* = event.data.putonitemData as ItemData;
            var _loc_3:* = event.data.getoffItemData as ItemData;
            if (_loc_2 && _loc_2.itemInfo.level > Cache.instance.newPet.pet.code)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(71773, _loc_2.itemInfo.level), MsgRollTipsType.msgRollTips3);
            }
            GameProxy.pet.dress(_loc_2 ? (_loc_2.serverData.uid) : (""), _loc_3 ? (_loc_3.serverData.uid) : (""));
            return;
        }// end function

        private function updateEquip(param1:Object) : void
        {
            if (this._moduel)
            {
                this._moduel.updateEquip();
            }
            return;
        }// end function

        private function updatePetItem(param1:Object) : void
        {
            if (this._moduel)
            {
                this._moduel.updatePetItems();
            }
            return;
        }// end function

        private function shapeCardStatUpdate(param1:Object) : void
        {
            return;
        }// end function

    }
}
